router = require './router'
rrr = new router.RouteRuleRegisters('')

rps = {
    None: "None"
    GetMeta: "GetMeta"
    EditMeta: "EditMeta"
    DeleteMeta: "DeleteMeta"
    ListEntity: "ListEntity"
    GetOneEntity: "GetOneEntity"
    CreateEntity: "CreateEntity"
    UpdateOneEntity: "UpdateOneEntity"
    UpdateManyEntity: "UpdateManyEntity"
    DeleteManyEntity: "DeleteManyEntity"
    RecoverManyEntity: "RecoverManyEntity"
    ChangePassword: "ChangePassword"
    ResetPassword: "ResetPassword"
    ChangePhone: "ChangePhone"
    ChangeEmail: "ChangeEmail"
    FileUpload: "FileUpload"
    FileUploadForRichText: "FileUploadForRichText"
    ConfigES: "ConfigES"
    PromotionManage: "PromotionManage"
    ListFilters: "ListFilters"
}

exports.rps = rps

exports.addRouteRules = ->
    AdminHandler = require('./http/AdminHandler')

    rrr.get '/admin-ui', {}, AdminHandler.gGetUiConfig

    # ======================================
    # 元数据管理
    # ======================================
    MetaHandler = require('./http/MetaHandler')

    rrr.get '/meta', {action: rps.GetMeta}, MetaHandler.gGetAllMeta
    rrr.get '/meta/entity/_empty', {action: rps.EditMeta}, MetaHandler.gGetEmptyEntityMeta
    rrr.put '/meta/:type/:name', {action: rps.EditMeta}, MetaHandler.gSaveMeta
    rrr.del '/meta/:type/:name', {action: rps.EditMeta}, MetaHandler.gRemoveMeta

    # ======================================
    # 用户
    # ======================================

    UserHandler = require './http/UserHandler'

    rrr.get '/ping', {}, UserHandler.gPing

    rrr.get '/sign-in', {isPage: true}, UserHandler.toSignInPage
    rrr.post '/api/sign-in', {}, UserHandler.gSignIn

    rrr.get '/sign-out', {action: rps.None, isPage: true}, UserHandler.toSignOutPage
    rrr.post '/api/sign-out', {action: rps.None}, UserHandler.gSignOut

    rrr.get '/change-password', {action: rps.ChangePassword, isPage: true}, UserHandler.toChangePasswordPage
    rrr.post '/api/change-password', {action: rps.ChangePassword}, UserHandler.gChangePassword

    rrr.get '/reset-password', {action: rps.ResetPassword, isPage: true}, UserHandler.toResetPasswordPage
    rrr.post '/api/reset-password', {action: rps.ResetPassword}, UserHandler.gResetPassword

    rrr.get '/change-phone', {action: rps.ChangePhone, isPage: true}, UserHandler.toChangePhonePage
    rrr.post '/api/change-phone', {action: rps.ChangePhone}, UserHandler.gChangePhone

    rrr.get '/change-email', {action: rps.ChangeEmail, isPage: true}, UserHandler.toChangeEmailPage
    rrr.post '/api/change-email', {action: rps.ChangeEmail}, UserHandler.gChangeEmail

    # ======================================
    # 安全
    # ======================================
    SecurityCodeHandler = require './http/SecurityCodeHandler'

    # 发送注册验证码到手机和邮箱
    rrr.post '/security-code/phone/:phone', {message: ''}, SecurityCodeHandler.sendSignUpCodeToPhone
    rrr.post '/security-code/email/:email', {message: ''}, SecurityCodeHandler.sendSignUpCodeToEmail

    CaptchaHandler = require './http/CaptchaHandler'
    # 请求一个图形验证码
    rrr.get '/captcha', {}, CaptchaHandler.generate

    # ======================================
    # 实体 CRUD
    # ======================================

    EntityHandler = require('./http/EntityHandler')
    ac = require('./http/AccessController')
    cea = ac.gControlEntityAccess

    rrr.get '/entity/:entityName', {action: rps.ListEntity}, cea, EntityHandler.gList
    rrr.get '/entity/:entityName/:id', {action: rps.GetOneEntity}, cea, EntityHandler.gFindOneById
    rrr.post '/entity/:entityName', {action: rps.CreateEntity}, cea, EntityHandler.gCreateEntity
    rrr.put '/entity/:entityName/:id', {action: rps.UpdateOneEntity}, cea, EntityHandler.gUpdateEntityById
    rrr.put '/entity/:entityName', {action: rps.UpdateManyEntity}, cea, EntityHandler.gUpdateEntityInBatch
    rrr.del '/entity/:entityName', {action: rps.DeleteManyEntity}, cea, EntityHandler.gDeleteEntityInBatch
    rrr.post '/entity/:entityName/recover', {action: rps.RecoverManyEntity}, cea, EntityHandler.gRecoverInBatch

    rrr.put '/entity/filters', {action: rps.ListFilters}, EntityHandler.gSaveFilters
    rrr.del '/entity/filters', {action: rps.ListFilters}, EntityHandler.gRemoveFilters

    # ======================================
    # 文件
    # ======================================

    UploadHandler = require('./http/UploadHandler')
    rrr.post '/file', {action: rps.FileUpload}, UploadHandler.gUpload # h5
    rrr.post '/file2', {action: rps.FileUpload}, UploadHandler.gUpload2 # transport
    rrr.post '/rich-text-file', {action: rps.FileUploadForRichText}, UploadHandler.gUploadForRichText # transport

    # ======================================
    # 搜索引擎
    # ======================================

    ElasticSearchController = require './http/ElasticSearchController'

    rrr.post '/config-es', {action: rps.ConfigES}, ElasticSearchController.gConfig

    # ======================================
    # 支付
    # ======================================

    TppiHandler = require './http/TppiHandler'

    rrr.post '/pay/api/callback/weixin', {}, TppiHandler.gProcessWeixinCallback
    rrr.post '/pay/api/callback/alipay', {}, TppiHandler.gProcessAlipayCallback
    # 支付宝前端回调
    # rrr.get '/pay/callback/alipay', {}, TppiHandler.toAlipayFrontCallback

    # ======================================
    # 推广活动
    # ======================================

    PromotionHandler = require './http/PromotionHandler'

    rrr.get '/pt/:name', {}, PromotionHandler.toPromotion
    rrr.del '/pt/_page-cache', {action: rps.PromotionManage}, PromotionHandler.gInvalidateStaticPageCache
    rrr.post '/pt/_persist-pv-now', {action: rps.PromotionManage}, PromotionHandler.gPersistPageViewNow
    rrr.get '/pt/_report/:promotion', {action: rps.PromotionManage}, PromotionHandler.gReport
    rrr.get '/pt/_channels/:promotion', {action: rps.PromotionManage}, PromotionHandler.gGetPromotionChannels
    rrr.put '/pt/_channels/:promotion', {action: rps.PromotionManage}, PromotionHandler.gPutPromotionChannels
